package com.feng.common.enums;

import lombok.Getter;

/**
 * 常用业务异常类型枚举
 *
 * @author Jack.Cheng
 * @date 2021/6/9 17:20
 */
public enum CommonErrorCode implements ErrorCode {

    APP_BIZ_ERROR("10000", "业务异常"),
    APP_PARAM_ERROR("10001", "请求参数错误"),
    APP_PARAM_NULL("10002", "请求参数为空"),
    APP_RETURN_NULL("10003", "返回数据为空"),
    APP_DATA_NULL("10004", "未查询到相关数据"),
    APP_PASSWORD_ERROR("10005", "密码错误"),
    APP_INVALIDATE_USER("10006", "非法用户"),
    USER_NOT_EXIST_CODE("2001", "用户不存在，请联系管理员！"),
    USER_FORBIDDEN_CODE("2002", "用户被禁用或已被删除，请联系管理员！"),
    BAD_REQUEST("400", "非法请求"),
    UNAUTHORIZED("401","用户未登录");

    CommonErrorCode(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 异常编码
     */
    @Getter
    private String code;
    /**
     * 异常消息
     */
    @Getter
    private String msg;

    public static CommonErrorCode getInstance(String code) {
        for (CommonErrorCode errorCode : values()) {
            if (errorCode.getCode().equals(code)) {
                return errorCode;
            }
        }
        return null;
    }

    /**
     * @param code error code
     * @return enum
     * @deprecated use {@link CommonErrorCode#getInstance(String)}
     */
    @Deprecated
    public CommonErrorCode getMsg(String code) {
        for (CommonErrorCode errorCode : values()) {
            if (errorCode.getCode().equals(code)) {
                return errorCode;
            }
        }
        return null;
    }
}
